import { IPlatform } from './IPlatform';

export class AppPlatform implements IPlatform {
  async chooseVideo(): Promise<string> {
    try {
      const [err, res] = await uni.chooseVideo({
        sourceType: ['album', 'camera'],
        compressed: false,
        maxDuration: 60
      });
      if (err) throw err;
      return res.tempFilePath;
    } catch (error) {
      throw new Error('选择视频失败');
    }
  }

  async saveSnapshot(canvas: HTMLCanvasElement): Promise<void> {
    try {
      const [err, res] = await uni.canvasToTempFilePath({
        canvasId: canvas.id,
        quality: 1,
        fileType: 'png'
      });
      
      if (err) throw err;

      await uni.saveImageToPhotosAlbum({
        filePath: res.tempFilePath
      });
      
      uni.showToast({ 
        title: '已保存到相册', 
        icon: 'success' 
      });
    } catch (error) {
      throw new Error('保存截图失败');
    }
  }

  seekVideo(videoElement: any, time: number): void {
    const videoContext = uni.createVideoContext(videoElement.id);
    videoContext.seek(time);
  }

  getVideoContext(videoElement: any): any {
    return uni.createVideoContext(videoElement.id);
  }

  initVideoListener(videoElement: any, callback: () => void): void {
    // APP端视频加载完成后直接调用回调
    callback();
  }
}